package com.zhika.zishi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhika.zishi.R;
import com.zhika.zishi.beans.AnswerOfProblemBean;
import com.zhika.zishi.util.LogUtil;

import org.w3c.dom.ProcessingInstruction;

import java.util.ArrayList;

/**
 * Created by Michael on 2015/4/8.
 */
public class AnswerOfProblemAdapter extends BaseAdapter {

    private static final String TAG = "AnswerOfProblemAdapter";

    private Context context;

    private ArrayList<AnswerOfProblemBean> answerList;
    public AnswerOfProblemAdapter(Context context,ArrayList<AnswerOfProblemBean> answerList){
        this.context = context;

        this.answerList = answerList;
        LogUtil.e(TAG,"回答的个数："+answerList.size());
    }

    @Override
    public Object getItem(int position) {
        return answerList.get(position);

    }

    @Override
    public int getCount() {
        return answerList.size();
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder holder;
        if (convertView == null){
            view = LayoutInflater.from(context).inflate(R.layout.answer_of_problem,null);
            holder = new ViewHolder();
            holder.headerImageView = (ImageView) view.findViewById(R.id.answer_of_problem_header_image_view);
            holder.nicknameText = (TextView) view.findViewById(R.id.answer_of_problem_nickname_text);
            holder.likeNumText = (TextView) view.findViewById(R.id.answer_of_problem_like_text);
            holder.contentText = (TextView) view.findViewById(R.id.answer_of_problem_content_text);
            holder.pictureImageView = (ImageView) view.findViewById(R.id.answer_of_problem_picture_image_view);
            view.setTag(holder);
        }else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
            resetViewHolder(holder);
        }

        AnswerOfProblemBean bean= (AnswerOfProblemBean) getItem(position);
        /**
         * 头像从url中获取，现在只是测试
         */
        String headerUrl = bean.getHeaderUrlStr();
        holder.headerImageView.setImageResource(R.drawable.ic_launcher);
        holder.nicknameText.setText(bean.getNickname());
        holder.likeNumText.setText(bean.getLikeNumStr());
        holder.contentText.setText(bean.getContentStr());
        holder.pictureImageView.setImageResource(R.drawable.test_1);

        return view;
    }

    /**
     * 重置View,防止重复item
     * @param holder
     */
    private void resetViewHolder(ViewHolder holder){
        holder.headerImageView.setImageResource(0);
        holder.nicknameText.setText(null);
        holder.likeNumText.setText(null);
        holder.contentText.setText(null);
        holder.pictureImageView.setImageResource(0);

    }

    private class ViewHolder{
        public ImageView headerImageView;

        public TextView nicknameText;

        public TextView likeNumText;

        public TextView contentText;

        public ImageView pictureImageView;

    }
}



